package ch.pizzapp.kurier.view;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import ch.pizzapp.kurier.R;
import ch.pizzapp.kurier.data.OrderItemData;

/**
 * Shanuka @Cyberlmj 3:08:30 PM Email: shanuka.gayashan@cyberlmj.com
 */
@EViewGroup(R.layout.oder_item)
public class OrderItem extends RelativeLayout {

	@ViewById(R.id.buttonAdd)
	ImageButton pulusButtons;

	@ViewById(R.id.buttonMinus)
	ImageButton minusButton;

	@ViewById(R.id.textViewQuntity)
	TextView textViewQuentity;

	@ViewById(R.id.textViewTitle)
	TextView textViewTitle;

	@ViewById(R.id.textViewSize)
	TextView textViewSize;

	@ViewById(R.id.textViewPrice)
	TextView textViewPrice;

	int selectQuentityValue = 1;

	private Callback mCallback;

	public interface Callback {
		void onClicked(int selectQuentityValue, Double calculatedValue,
				Integer itemId);

		void deleteFromCart();

	}

	public void setOnCallBack(Callback callback) {
		mCallback = callback;

	}

	public OrderItem(final Context context) {
		super(context);

	}

	@AfterViews
	void afterView() {

	}

	public OrderItem(final Context context, final AttributeSet attrs) {
		super(context, attrs);
	}

	@Click({ R.id.buttonAdd, R.id.buttonMinus, R.id.imageViewDelete, })
	void onClick(View vuew) {
		switch (vuew.getId()) {
		case R.id.buttonAdd:

			puls();
			break;
		case R.id.buttonMinus:

			minus();
			break;

		case R.id.imageViewDelete:

			mCallback.deleteFromCart();
			break;

		default:
			break;
		}
	}

	void buttonEnable(int selectVale) {
		switch (selectVale) {
		case 1:
			minusButton.setEnabled(false);

			break;
		case 99:
			pulusButtons.setEnabled(false);
			break;

		default:
			pulusButtons.setEnabled(true);
			minusButton.setEnabled(true);
			break;
		}
	}

	void puls() {

		if (selectQuentityValue < 99) {
			selectQuentityValue = selectQuentityValue + 1;
			textViewQuentity.setText(String.valueOf(selectQuentityValue));
			// mCallback.onClicked(selectVale);
			calculatePrice(selectQuentityValue);
			buttonEnable(selectQuentityValue);

		}
	}

	void minus() {

		if (selectQuentityValue > 1) {
			selectQuentityValue = selectQuentityValue - 1;
			textViewQuentity.setText(String.valueOf(selectQuentityValue));
			// mCallback.onClicked(selectVale);
			calculatePrice(selectQuentityValue);
			buttonEnable(selectQuentityValue);
		}

	}

	OrderItemData orderItemData;

	public void bind(final OrderItemData orderItemData) {

		this.selectQuentityValue = orderItemData.getQuentity();

		this.orderItemData = orderItemData;

		textViewTitle.setText(orderItemData.getTitle());

		textViewSize.setText(orderItemData.getSize());

		textViewPrice.setText(String.valueOf(orderItemData.getPrice()));

		textViewQuentity.setText(String.valueOf(selectQuentityValue));

		//calculatePrice(selectQuentityValue);

	}

	void calculatePrice(int selectQuentityValue) {

		Double calculatedValue = selectQuentityValue
				* orderItemData.getItemPrice();
		if (mCallback != null) {
			mCallback.onClicked(selectQuentityValue, calculatedValue,
					orderItemData.getItemID());
		}
		textViewPrice.setText(String.valueOf(calculatedValue));
	}

}
